package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理文章相关得业务类
 */
public class ArticleController {
    //处理发表文章得方法
    //保存素所有用户信息的目录名字
    private static String articleDirName = "./articles/";


    static  {
        File articleDir = new File(articleDirName);//读内容
        if (!articleDir.exists()){//判断文件是否存在
            articleDir.mkdirs();//没有存在的话创建一个新文件
        }
    }
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("处理发表文章...");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");


        if(title==null||author==null||content==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }
        try(
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleDirName+title+".obj")
                );
        ){
            Article article = new Article(title,content,author);
            oos.writeObject(article);
            //响应发表成功页面
            response.setEntity(new File("./webapps/myweb/article_success.html"));
        }catch(IOException e){
            e.printStackTrace();
        }


        System.out.println("发表文章处理完毕!");
    }

    /**
     * 读取所有的文章并生成动态页面
     */
    public void showAllArticl(HttpResponse response,HttpRequest request){
        System.out.println("开始生成所有文章的动态页面...");

        List<Article> arrayList = new ArrayList<>();

        File articleFile = new File(articleDirName);
        File[] subs = articleFile.listFiles(f->f.getName().endsWith(".obj"));
        for(File userFile : subs){
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(userFile)
                    )
            ){
                arrayList.add((Article) ois.readObject());

            }catch (IOException | ClassNotFoundException e){
                e.printStackTrace();
            }
        }

        arrayList.forEach(System.out::println);


        PrintWriter pw = response.getWrite();
      pw.println("<!DOCTYPE html>");
      pw.println("<html lang=\"en\">");
      pw.println("<head>");
      pw.println("<meta charset=\"UTF-8\">");
      pw.println("<title>文章列表</title>");
      pw.println("</head>");
      pw.println("<body>");
      pw.println("<center>");
      pw.println("<h1>文章列表</h1>");
      pw.println("<table border=\"1\">");
      pw.println("<tr>");
      pw.println("<td>标题</td>");
      pw.println("<td>作者</td>");
      pw.println("</tr>");
        //遍历输入 已经存在/注册的用户
        for (Article article : arrayList) {
            pw.println(" <tr> ");
            pw.println("<td>"+article.getWriteArtice()+"</td>");
            pw.println("<td>"+article.getAuthor()+"</td>");
//            pw.println("<td>"+article.getTitle()+"</td>");

            pw.println("</tr>");
        }
        pw.println("  </table>");
        pw.println(" </center> ");
        pw.println("  </body> ");
        pw.println("</html> ");
        //设置正文类型

        response.setContentType("text1/html");

        System.out.println("生成完毕");


    }

}



